<?php
/**
 * Created by PhpStorm.
 * User: 帅帅吴
 * Date: 2018/11/5
 * Time: 9:28
 */

namespace app\admin\model;


class ShopOrder extends DataBase
{
    protected $name = 'shop_order';
    protected $autoWriteTimestamp=true;

    public function getTypeAttr($value)
    {
        $status = [1=>'座驾',2=>'消耗品',3=>'其他'];
        return $status[$value];
    }

    public function getStatusAttr($value)
    {
        $status = [1=>'普通',2=>'非卖'];
        return $status[$value];
    }

    public function getDataList()
    {
        //条件
        $page_config = array(
            'page' => input('offset', 1),
            'limit' => input('limit', 10)
        );
        $where = [];
        if (!empty(input('start_time')) && !empty(input('end_time'))) {
            $start = strtotime(input('start_time'));
            $end = strtotime(input('end_time'));
            $where[] = ['ss_shop_order.create_time', 'between', [$start, $end]];
        }
        if (!empty(input('key')) && !empty(input('value'))) {
            if (input('key') == 'shop_order_id') {
                $where[] = ['ss_shop_order.' . input('key'), '=', input('value')];
            } elseif (input('key') == 'u_id') {
                $where[] = ['ss_users.' . input('key'), '=', input('value')];
            } elseif (input('key') == 'type') {
                switch (trim(input('value'))) {
                    case '座驾':
                        $where[] = ['ss_shop_order.' . input('key'), '=', 1];
                        break;
                    case '消耗品':
                        $where[] = ['ss_shop_order.' . input('key'), '=', 2];
                        break;
                    default:
                        $where[] = ['ss_shop_order.' . input('key'), '=', 3];
                }
            } elseif (input('key') == 'status') {
                switch (trim(input('value'))) {
                    case '普通':
                        $where[] = ['ss_shop_order.' . input('key'), '=', 1];
                        break;
                    case '非卖':
                        $where[] = ['ss_shop_order.' . input('key'), '=', 2];
                        break;
                    default:
                        $where[] = [];
                }
            }
        }
        $order = input('sort', 'ss_shop_order.shop_order_id') . ' ' . input('order', 'asc');

        //查寻
        $data = $this
            ->view('ss_shop_order', 'shop_order_id,type,status,number,product_id,create_time')
            ->view('ss_users', 'u_id', 'ss_shop_order.user_id = ss_users.id')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
        foreach ($data as $key => $value) {
            if ($value['type'] == '座驾') {
                $data[$key]['product_name'] = Vehicle::where(['vehicle_id' => $value['product_id']])->value('vehicle_name');
                continue;
            }
            if ($value['type'] == '消耗品') {
                $data[$key]['product_name'] = ShopHorn::where(['horn_id' => $value['product_id']])->value('horn_name');
            }
        }
        return $data;
    }
}